package service

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
)

// 获取文章分类
func ArticleCategoryList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MArticleCategory
	var count int64
	tx := models.DB.Model(new(models.MArticleCategory)).Where("aid=?", jsonData.Aid)
	if jsonData.Id > 0 {
		tx = tx.Where("pid=?", jsonData.Id)
	}
	tx = tx.Count(&count).Order("sort ASC,id").Find(&list)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 获取商品列表
type ShopJson struct {
	Id     int    `json:"id"`
	Aid    int    `json:"aid"`
	Bid    int    `json:"bid"`    //商户ID
	Status int    `json:"status"` //状态0未上架1已上架
	Name   string `json:"name"`   //商品名称
	Cid    int    `json:"cid"`    //分类id，可存储多分类用,间隔
	Gid    int    `json:"gid"`    //分组ID多个用逗号分割
	Page   int    `json:"page"`
	Limit  int    `json:"limit"`
}
type ArticleData struct {
	ID          int    `gorm:"type:int(11); primaryKey" json:"id"`
	Aid         int    `gorm:"type:int(11);" json:"aid"`          //商户id
	Bid         int    `gorm:"type:int(11);" json:"bid"`          //用户id
	Cid         int    `gorm:"type:int(11);" json:"cid"`          //用户id
	Name        string `gorm:"type:varchar(255);" json:"name"`    //标题
	Subname     string `gorm:"type:varchar(255);" json:"subname"` //副标题
	Pic         string `json:"pic"`                               //缩略图
	Author      string `json:"author"`                            //作者
	Content     string `json:"content"`                           //内容
	Readcount   int    `json:"readcount"`                         //阅读量
	Status      int    `json:"status"`                            //状态0不显示1显示
	Sort        int    `json:"sort"`                              //排序 越大越靠前
	Createtime  int    `json:"createtime"`                        //创建时间
	Sendtime    string `json:"sendtime"`                          //发布时间
	Reason      string `json:"reason"`                            //
	CateName    string `json:"cate_name"`
	CateSubname string `json:"cate_subname"`
}

// 获取文章内容
func ArticleList(c *gin.Context) {
	var jsonData ShopJson
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []ArticleData
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	selectData := "m_article.id,m_article.bid,m_article.aid,m_article.cid,m_article.name,m_article.subname,m_article.pic,m_article.author,m_article.content" +
		",m_article.readcount,m_article.status,m_article.sort,m_article.createtime,m_article.sendtime,m_article.reason,m_article_category.name as cate_name" +
		",m_article_category.sub_name as cate_subname"
	tx := models.DB.Table("m_article").Select(selectData).Joins("left join m_article_category on m_article_category.id=m_article.cid").Where("m_article.aid=?  AND m_article.name LIKE ?", jsonData.Aid, "%"+jsonData.Name+"%")
	tx = tx.Where("m_article.status=?", 1)

	if jsonData.Id > 0 {
		tx = tx.Where("m_article.id = ?", jsonData.Id)
	}
	if jsonData.Cid > 0 {
		tx = tx.Where("m_article.cid = ?", jsonData.Cid)
	}
	if jsonData.Gid > 0 {
		tx = tx.Where("m_article.pcid = ?", jsonData.Gid)
	}
	tx = tx.Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("m_article.sort ASC,m_article.id").Find(&list)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 获取文章分类
func GetArticleCategoryList(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var list []models.MArticleCategory
	var count int64
	tx := models.DB.Model(new(models.MArticleCategory)).Where("aid=?", jsonData.Aid)
	if jsonData.Id > 0 {
		tx = tx.Where("id=?", jsonData.Id)
	}
	tx = tx.Count(&count).Order("sort ASC,id").Find(&list)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10001,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 文章详情
func GetArticleShow(c *gin.Context) {
	var jsonData models.MArticle
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var data models.MArticle
	err = models.DB.Model(new(models.MArticle)).Where("id=?", jsonData.ID).First(&data).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonData, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data":    data,
	})
}

// 新增阅读量
func AddYuedu(c *gin.Context) {
	var jsonData models.MArticle
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	err = models.DB.Model(new(models.MArticle)).Where("id=?", jsonData.ID).Update("readcount", gorm.Expr("readcount+?", 1)).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonData, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data":    "",
	})
}
